package net.zjitc.controller.admin;

import net.zjitc.common.RespMsg;
import net.zjitc.common.ServiceResp;
import net.zjitc.mapper.NoticeMapper;
import net.zjitc.pojo.Notice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/4/1 9:08
 */
@Controller
@RequestMapping("/admin/notice")
public class NoticeController {
    @Autowired
    NoticeMapper noticeMapper;

    @RequestMapping(value = "/findAll")
    public String findAll(Model model)
    {
        List<Notice> list = noticeMapper.findAll();
        model.addAttribute("list",list);
        return "/admin/notices/list.jsp";
    }

    @RequestMapping(value = "/findById")
    public String findById(int id,Model model){
        Notice notice = noticeMapper.findById(id);
        model.addAttribute("n",notice);
        return "/admin/notices/edit.jsp";
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public ServiceResp update(@RequestBody Notice notice){
        int update = noticeMapper.update(notice);
        if (update>0)
        {
            return ServiceResp.createBySuccess("修改成功",0);
        }
        return ServiceResp.createByERROR("修改失败",1);
    }

    @RequestMapping(value = "/delete")
    public String delete(int id){
        noticeMapper.delete(id);
        return "redirect:/admin/notice/findAll";
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    public ServiceResp add(@RequestBody Notice notice){
        int add = noticeMapper.add(notice);
        if (add>0)
        {
            return ServiceResp.createBySuccess("添加成功",0);
        }
        return ServiceResp.createByERROR("添加失败",1);
    }
}
